package com.cn.yz.service.sysauthority.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cn.yz.common.httpentity.ResponseObject;
import com.cn.yz.common.httpentity.ResponseString;
import com.cn.yz.common.page.Page;
import com.cn.yz.common.page.PagedData;
import com.cn.yz.common.utils.EncryptUtil;
import com.cn.yz.service.sysauthority.model.SysUser;
import com.cn.yz.service.sysauthority.service.SysUserService;

@RestController
@RequestMapping("sysUser")
public class SysUserController {
	
	private static Logger LOG = LoggerFactory.getLogger(SysUserController.class);

	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 新增会员
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value="/add",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
	public ResponseString add(@RequestBody SysUser sysUser){
		LOG.info("SysUserController.add");
		String accountEncrypt = sysUser.getAccount();
		String account = EncryptUtil.decryptBase64(accountEncrypt);
		sysUser.setAccount(account);
		
		String passwordEncrypt = sysUser.getPassword();
		String password = EncryptUtil.decryptBase64(passwordEncrypt);
		sysUser.setPassword(password);
		sysUserService.addSysUser(sysUser);
		return new ResponseString();
	}
	
	/**
	 * 修改会员
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value="/modify",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
	public ResponseString modify(@RequestBody SysUser sysUser){
		LOG.info("SysUserController.modify");
		sysUserService.modSysUser(sysUser);
		return new ResponseString();
	}
	
	/**
	 * 删除会员
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value="/delete",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
	public ResponseString delete(@RequestBody SysUser sysUser){
		LOG.info("SysUserController.delete");
		sysUserService.delSysUser(sysUser.getId());
		return new ResponseString();
	}
	
	/**
	 * 根据id获取会员信息
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value="/getById/{id}",method = RequestMethod.GET)
	public ResponseObject<SysUser> getById(@PathVariable Integer id){
		LOG.info("SysUserController.getSysUser");
		SysUser sysUser = sysUserService.selectByPrimaryKey(id);
		return new ResponseObject<SysUser>(sysUser);
	}
	
	/**
	 * 根据账号获取会员信息
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/getByAccount",method = RequestMethod.GET)
	public ResponseObject<SysUser> getByAccount(String account){
		LOG.info("SysUserController.getSysUser");
		SysUser sysUser = sysUserService.selectSysUserByAccount(EncryptUtil.decryptBase64(account));
		return new ResponseObject<SysUser>(sysUser);
	}
	
	/**
	 * 根据账号获取会员信息
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public ResponseObject<PagedData<SysUser>> list(@RequestParam(required=false) String nickName,Integer isTestuser,Integer start,Integer limit){
		LOG.info("SysUserController.getSysUser");
		Map<String,Object> condition = new HashMap<String,Object>();
		if(!StringUtils.isEmpty(nickName)){
			condition.put("nickName", nickName);
		}
		if(isTestuser != null){
			condition.put("isTestuser", isTestuser);
		}
		
		Page page = null;
		if(start > 0 && limit > 0){
			page = new Page(start,limit);
		}
		
		PagedData<SysUser> pageDate = sysUserService.selectSysUserList(condition, page);
		return new ResponseObject<PagedData<SysUser>>(pageDate);
	}
}
